package com.strawcom.charmring.bluetooth;

import android.content.*;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.TextView;
import com.strawcom.charmring.App;
import com.strawcom.charmring.BaseActivity;
import com.strawcom.charmring.C;
import com.strawcom.charmring.R;
import com.strawcom.charmring.bluetooth.RingBleService.LocalBinder;
import com.strawcom.charmring.bluetooth.upgrade.UpdateService;
import com.strawcom.charmring.util.BaseUtil;
import com.strawcom.charmring.util.LogUtil;
import com.strawcom.charmring.util.PreferenceUtil;
import com.strawcom.charmring.view.TitleView;

import java.io.File;

/**
 * @author WangPeng
 */
public class RingBleUpgradeActivity extends BaseActivity {

	private TitleView titleView;
	private TextView txImgVersion , txImgName;
	
	private RingBleService bleService;

	@Override protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_ring_ble_upgrade);
		super.onCreate(savedInstanceState);

		titleView = (TitleView) findViewById(R.id.view_title);
		titleView.setTitleText(R.string.version);

		bindBleService();
		registerRingMessageReceiver();
		registerUpgradeBleDeviceReceiver();

		File f = RingBleController.obtainBleUpdateFile(this);
		LogUtil.e("升级路径：" + f.getAbsolutePath());

		TextView txAppVersion = (TextView) findViewById(R.id.tx_app_version);
		String currVersion = BaseUtil.getAppVersionName(this);
		txAppVersion.setText(currVersion);
		TextView txUserPhone = (TextView) findViewById(R.id.tx_user_phone);
		String userPhone ="+" + " " + PreferenceUtil.readString(App.i(), C.sp.phone);
		txUserPhone.setText(userPhone);
		
		TextView txBleHVersion = (TextView) findViewById(R.id.tx_ble_h_version);
		
		TextView txBleSVersion = (TextView) findViewById(R.id.tx_ble_s_version);
		
		TextView txBleMac = (TextView) findViewById(R.id.tx_ble_mac);
		
		txImgVersion = (TextView) findViewById(R.id.tx_ble_img_version);
		
		txImgName = (TextView) findViewById(R.id.tx_ble_img_name);
		
		findViewById(R.id.btn_ble_upgrade).setOnClickListener(new View.OnClickListener() {
			@Override public void onClick(View v) {
				if(bleService == null){
					showToast(R.string.binding_ble_1);
				} else {
					doUpgradeBle();
				}
			}
		});
		
		String a = "/storage/emulated/0/Android/data/com.strawcom.charmring/files/update_ring.img";
	}
	
	
	
	private void doUpgradeBle() {
		// from local ; from internet ;
		upgradeBleDevice();
	}

	@Override protected void onDestroy() {
		super.onDestroy();
		unbindBleService();
		unregisterRingMessageReceiver();
		unregisterUpgradeBleDeviceReceiver();
	}

	private void bindBleService() {
		Intent intent = new Intent(this, RingBleService.class);
		bindService(intent, conn, BIND_AUTO_CREATE);
	}

	private void unbindBleService() {
		unbindService(conn);
	}

	private final ServiceConnection conn = new ServiceConnection() {
		@Override public void onServiceDisconnected(ComponentName name) {
			LogUtil.e("onServiceDisconnected()");
		}

		@Override public void onServiceConnected(ComponentName name, IBinder service) {
			LogUtil.e("onServiceConnected()");
			bleService = ((LocalBinder) service).getService();

		}
	};

	private void registerRingMessageReceiver() {
		IntentFilter filter = new IntentFilter();
		filter.addAction(RingBleService.ACTION_GATT_CONNECTED);
		filter.addAction(RingBleService.ACTION_GATT_DISCONNECTED);
		filter.addAction(RingBleService.ACTION_GATT_VERSION_INFO);

		registerReceiver(ringMessageReceiver, filter);
	}

	private void unregisterRingMessageReceiver() {
		unregisterReceiver(ringMessageReceiver);
	}

	private final BroadcastReceiver ringMessageReceiver = new BroadcastReceiver() {
		@Override public void onReceive(Context context, Intent intent) {
			final String action = intent.getAction();
			if (RingBleService.ACTION_GATT_CONNECTED.equals(action)) {
			}
			else if (RingBleService.ACTION_GATT_DISCONNECTED.equals(action)) {
			}
			else if (RingBleService.ACTION_GATT_VERSION_INFO.equals(action)) {
			}
		}
	};

	private void upgradeBleDevice() {
		if (bleService != null) {
			bleService.requestDeviceUpgrade(bleService.getCurrAddress(), RingBleController.UPGRADE_1);
		}
	}

	private void registerUpgradeBleDeviceReceiver() {
		IntentFilter filter = new IntentFilter();
		filter.addAction(UpdateService.ACTION_DOWN_OVER);
		registerReceiver(upgradeBleDeviceReceiver, filter);
	}

	private void unregisterUpgradeBleDeviceReceiver() {
		unregisterReceiver(upgradeBleDeviceReceiver);
	}

	private final BroadcastReceiver upgradeBleDeviceReceiver = new BroadcastReceiver() {
		@Override public void onReceive(Context context, Intent intent) {
			final String action = intent.getAction();
			if (UpdateService.ACTION_DOWN_OVER.equals(action)) {
				if (PreferenceUtil.readBoolean(RingBleUpgradeActivity.this, C.sp.is_downing_ble_file)) {
					int result = intent.getIntExtra(UpdateService.EXTRA_DATA, UpdateService.DOWN_ERROR);
					if (UpdateService.DOWN_ERROR == result) {
					}
					else if (UpdateService.DOWN_OK == result) {
					}
				}
			}
		}
	};

}
